macroScript OBJExport category:"AirCrash"
(
	local faceVertexToNormalIndex = #()
	
	function writeMeshVertices objectToExport snapshotMesh objFile =
	(
		selSkin = objectToExport.modifiers[#Skin]
		if selSkin != undefined then(
			max modify mode
			modPanel.setCurrentObject selSkin
		)
		
		format "\n#% vertices to come\n" snapshotMesh.numverts to:objFile
		
		for vindex=1 to snapshotMesh.numverts do(
			vertex = getVert snapshotMesh vindex
			format "v % % %" vertex.x vertex.y vertex.z to:objFile
			count = skinops.getVertexWeightCount selSkin vindex
			totalweight = 0
			for countIndex = 1 to count  do
				totalweight += skinops.getVertexWeight selSkin vindex countIndex
			remainder = 1-totalweight
			for  countIndex = 1 to count  do
			(
				boneid = skinops.getVertexWeightBoneId selSkin vindex countIndex
				selBone = getNodeByName (skinops.getBoneName selSkin boneid 1)
				id=getUserProp selBone "id"
				weight = skinops.getVertexWeight selSkin vindex countIndex + remainder/count
				format " % %"  id weight to:objFile
			)
			if ( count < 4 ) then (
				for countIndex = count+1 to 4 do
				(
					format " 0.0 0.0" to:objFile
				)
			)
			format "\n" to:objFile
		)
	)
	
	function writeMeshTextureCoordinates snapshotMesh objFile =
	(
		format "\n#% Texture coordinates\n" snapshotMesh.numtverts to:objFile
		
		for tindex = 1 to snapshotMesh.numtverts do(
			texcoord = getTvert snapshotMesh tindex
			format "vt % %\n" (1-texcoord.y) texcoord.x to:objFile
		)
	)
	
	function writeMeshNormal snapshotMesh faceIndex vertexIndex normal objFile=
	(
		format "\nvn % % %" normal.x normal.y normal.z to:objFile
	)
	
	-- Write the normals to the objFile. Normals can be shared among vertices.
	-- snapshotMesh the mesh to write the vertices for.
	-- objFile the the file to write the vertices to.
	function writeMeshNormals snapshotMesh objFile =
	(
		nindex = 1
		for findex=1 to snapshotMesh.numfaces do(
			
			-- get all the normals for one triangle
			facenormals = meshop.getFaceRNormals snapshotMesh findex
			-- get all the vertices associated with that triangle.
			face = getFace snapshotMesh findex
			writeMeshNormal snapshotMesh findex 1 facenormals[1] objFile
			writeMeshNormal snapshotMesh findex 2 facenormals[2] objFile
			writeMeshNormal snapshotMesh findex 3 facenormals[3] objFile
			
			faceVertexToNormalIndex[findex] = [nindex, nindex+1, nindex+2]
			nindex += 3
		)
	)
	
	function writeMeshFaces snapshotMesh objFile = 
	(
		format "\n#% number of faces\n" snapshotMesh.numfaces to:objFile

		for findex = 1 to snapshotMesh.numfaces do(
			face = getFace snapshotMesh findex
			tcFace = getTVFace snapshotMesh findex
			
			f1 = (Integer)face.x
			f2 = (Integer)face.y
			f3 = (Integer)face.z
				
			tc1 = (Integer) tcFace.x
			tc2 = (Integer) tcFace.y
			tc3 = (Integer) tcFace.z
				
			normal = faceVertexToNormalIndex[findex]
			if (normal != undefined) then(
				normal1 = (Integer) normal.x
				normal2 = (Integer) normal.y
				normal3 = (Integer) normal.z
			)
				
			format "f %/%/% %/%/% %/%/%\n"  f1 tc1 normal1 f2 tc2 normal2   f3 tc3 normal3  to:objFile
		)
	)
	
	on execute do
	(
		output_name = getSaveFileName caption:"OBJ Mesh" types:"Obj (*.obj)|*.obj|"
	
		if output_name != undefined then 
		(
			output_file = createfile output_name

			format "#OBJ Exporter created by Ruben Rigole\n" to:output_file
			
			snapshotMesh = snapshot $Mesh
			
			writeMeshVertices $Mesh snapshotMesh output_file
			writeMeshTextureCoordinates snapshotMesh output_file
			writeMeshNormals snapshotMesh output_file
			writeMeshFaces snapshotMesh output_file
			
			close output_file
			delete snapshotMesh
		)
		print "DONE"
	)
)